// pages/orders/orders.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders:[],
    type:0,
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    wx.request({
      url: app.globalData.baseUrl + '/api/order/myOrders',
      data: {
        openid: app.globalData.openid,
        state:that.data.type,
        page:that.data.page
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          orders:res.data.data
        })
      }
    });
  },
  orderType:function(e){
    var that=this
    that.setData({
      type:e.currentTarget.dataset.type,
      page:1
    })
    wx.request({
      url: app.globalData.baseUrl + '/api/order/myOrders',
      data: {
        openid: app.globalData.openid,
        state:that.data.type,
        page:that.data.page
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        that.setData({
          orders:res.data.data
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.data.page = that.data.page + 1
    wx.request({
      url: app.globalData.baseUrl + '/api/order/myOrders',
      data: {
        openid: app.globalData.openid,
        state:that.data.type,
        page:that.data.page
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.code == 500) {
          wx.showToast({
            title: '没有新的数据了！',
            icon: 'none'
          })
        }
        if(res.data.code==200){
          for (var j = 0; j < res.data.res.length; j++) {
            that.data.orders.push(res.data.res[j])
          }
          that.setData({
            orders: that.data.orders
          })
        }
      }
    });

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})